package com.flute.haflute.jobbox.master;

import java.io.Serializable;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.haflute.jobbox.base.StatusReporter;
import com.flute.haflute.jobbox.communication.messages.CloudJobReqMessage;

public class CloudJob implements StatusReporter, Serializable {
	private static final long serialVersionUID = 1900895664219314633L;

	private static Logger logger = LoggerFactory.getLogger(CloudJob.class);

	private String statusDesc = "";

	private JOB_STATUS status = JOB_STATUS.UNKOWN;

	/** 当前工作的请求消息 */
	private CloudJobReqMessage jobRequest = null;
	/**  */
	private boolean isScalable = false;

	public String createTime;
	public String finishTime;

	public boolean isScalable() {
		return isScalable;
	}

	public void setScalable(boolean isScalable) {
		this.isScalable = isScalable;
	}

	public CloudJob(CloudJobReqMessage jobRequest) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.createTime = sdf.format(new java.util.Date(System
				.currentTimeMillis()));
		this.jobRequest = jobRequest;
	}

	@Override
	public synchronized void setStatus(JOB_STATUS status, String statusDesc) {
		this.status = status;
		this.statusDesc = statusDesc;
		logger.info("Job:{} status changed to {}", jobRequest.getJobConf()
				.getJobId(), JOB_STATUS.RUNNING.toString());
	}

	@Override
	public String getStatusDesc() {
		return statusDesc;
	}

	public CloudJobReqMessage getJobRequestMessage() {
		return jobRequest;
	}

	public boolean isRunning() {
		return status == JOB_STATUS.RUNNING;
	}

	public synchronized JOB_STATUS getStatus() {
		return status;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((jobRequest == null) ? 0 : jobRequest.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CloudJob other = (CloudJob) obj;
		if (jobRequest == null) {
			if (other.jobRequest != null)
				return false;
		} else if (!jobRequest.equals(other.jobRequest))
			return false;
		return true;
	}

}
